package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.SensitiveWordsMapper;
import com.qingcheng.pojo.comment.SensitiveWords;
import com.qingcheng.service.comment.CommmentServiceByWKF;
import com.qingcheng.util.AllComment;
import com.qingcheng.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = CommmentServiceByWKF.class)
public class CommmentServiceByWKFImpl implements CommmentServiceByWKF {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SensitiveWordsMapper sensitiveWordsMapper;
    @Autowired
    private IdWorker idWorker;

    //1.过滤敏感词汇
    @Override
    public String filterSensitiveWords(String content) {
        if (content == null || content.length() <= 0) {
            return content;
        }
        Set<SensitiveWords> sensitiveWordsSet = (Set<SensitiveWords>) redisTemplate.boundSetOps(AllComment.AllSitiveWords).members();
        String o = (String) redisTemplate.boundValueOps(AllComment.ShowSitiveWordsIsNull).get();
        try {
            if (!"0".equals(o)) {
                if (sensitiveWordsSet == null || sensitiveWordsSet.size() <= 0) {
                    sensitiveWordsSet = new HashSet<SensitiveWords>();
                    //从数据库导出数据,并再此存入redis
                    //判断内存里的数据是否为空,为空则重新加载一次
                    List<SensitiveWords> tempSensitiveWords = sensitiveWordsMapper.selectAll();
                    if (tempSensitiveWords.size() > 0 && tempSensitiveWords != null) {
                        //说明数据库有值,设置下状态
                        redisTemplate.boundValueOps(AllComment.ShowSitiveWordsIsNull).set("1");
                        //循环遍历,存入redis
                        for (SensitiveWords sensitiveWord : tempSensitiveWords) {
                            sensitiveWordsSet.add(sensitiveWord);
                            redisTemplate.boundSetOps(AllComment.AllSitiveWords).add(sensitiveWord);
                        }
                    } else {
                        redisTemplate.boundValueOps(AllComment.ShowSitiveWordsIsNull).set("0");
                        throw new RuntimeException("数据库敏感词汇为空");
                    }
                }
            } else {
                throw new RuntimeException("数据库敏感词汇为空");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        //执行过滤敏感词汇操作
        for (SensitiveWords sensitiveWords : sensitiveWordsSet) {
            //敏感词汇确定引用的进行过滤
            if ("1".equals(sensitiveWords.getStatus())) {
                String word = sensitiveWords.getSensitiveWords();
                if (content.contains(word)) {
                    content = content.replaceAll(word, "***");
                }
            }
        }
        return content;
    }

    //2.增加敏感词汇
    @Override
    @Transactional
    public boolean addSensitiveWords(String content) {
        if (content != null && content.length() >= 0) {
            try {
                //Set<SensitiveWords> sensitiveWordsSet = (Set<SensitiveWords>) redisTemplate.boundSetOps(AllComment.AllSitiveWords).members();
                //表示正在使用
                String o = (String) redisTemplate.boundValueOps(AllComment.addSitiveWords).get();
                if ("0".equals(o)) {
                    redisTemplate.boundValueOps(AllComment.addSitiveWords).set("1");
                    List<SensitiveWords> sensitiveWordsList = sensitiveWordsMapper.selectAll();
                    for (SensitiveWords sensitiveWords : sensitiveWordsList) {
                        if (content.equals(sensitiveWords.getSensitiveWords())) {
                            throw new RuntimeException("该字段已经存在");
                        }
                    }
                    SensitiveWords sensitiveWords = new SensitiveWords();
                    sensitiveWords.setId(idWorker.nextId() + "");
                    sensitiveWords.setSensitiveWords(content);
                    sensitiveWords.setStatus("1");
                    sensitiveWordsMapper.insert(sensitiveWords);
                    redisTemplate.boundValueOps(AllComment.ShowSitiveWordsIsNull).set("1");
                    redisTemplate.boundSetOps(AllComment.AllSitiveWords).add(sensitiveWords);
                    redisTemplate.boundValueOps(AllComment.addSitiveWords).set("0");
                    return true;
                } else {
                    throw new RuntimeException("正在增加字段,请等待");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return true;
        }
    }

    //3.设置不使用敏感词汇
    @Override
    @Transactional
    public boolean deleteSensitiveWords(String content) {
        if (content != null && content.length() >= 0) {
            try {
                String o = (String) redisTemplate.boundValueOps(AllComment.deleteSitiveWords).get();
                if ("0".equals(o)) {
                    redisTemplate.boundValueOps(AllComment.deleteSitiveWords).set("1");
                    SensitiveWords sensitiveWords = new SensitiveWords();
                    sensitiveWords.setSensitiveWords(content);
                    List<SensitiveWords> select = sensitiveWordsMapper.select(sensitiveWords);
                    for (SensitiveWords words : select) {
                        //设置不使用
                        words.setStatus("0");
                        sensitiveWordsMapper.updateByPrimaryKey(words);
                    }
                    redisTemplate.boundValueOps(AllComment.deleteSitiveWords).set("0");
                    return true;
                } else {
                    throw new RuntimeException("正在删除字段,请等待");
                }
            } catch (RuntimeException e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return true;
        }
    }

    //4.恢复使用敏感词汇
    @Override
    @Transactional
    public boolean returnSensitiveWords(String content) {
        if (content != null && content.length() >= 0) {
            try {
                String o = (String) redisTemplate.boundValueOps(AllComment.returnSitiveWords).get();
                if ("0".equals(o)) {
                    redisTemplate.boundValueOps(AllComment.returnSitiveWords).set("1");
                    SensitiveWords sensitiveWords = new SensitiveWords();
                    sensitiveWords.setSensitiveWords(content);
                    List<SensitiveWords> select = sensitiveWordsMapper.select(sensitiveWords);
                    for (SensitiveWords words : select) {
                        //设置不使用
                        words.setStatus("1");
                        sensitiveWordsMapper.updateByPrimaryKey(words);
                    }
                    redisTemplate.boundValueOps(AllComment.returnSitiveWords).set("0");
                    return true;
                } else {
                    throw new RuntimeException("正在删除字段,请等待");
                }
            } catch (RuntimeException e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return true;
        }
    }

    //5.显示所有敏感词汇 新增SensitiveWords对象,里面含有id和敏感词汇字段
    @Override
    public List<SensitiveWords> selectAllSensitiveWords() {
        String o = (String) redisTemplate.boundValueOps(AllComment.selectAllSitiveWords).get();
        Set<SensitiveWords> sensitiveWordsSet = (Set<SensitiveWords>) redisTemplate.boundSetOps(AllComment.AllSitiveWords).members();
        List<SensitiveWords> sensitiveWordsList = new ArrayList<SensitiveWords>();
        String redisDataShow = (String) redisTemplate.boundValueOps(AllComment.ShowSitiveWordsIsNull).get();
        try {
            if ("0".equals(o)) {
                    if (!"0".equals(redisDataShow)) {
                        redisTemplate.boundValueOps(AllComment.selectAllSitiveWords).set("1");
                        if (sensitiveWordsSet != null && sensitiveWordsSet.size() >= 0) {
                            for (SensitiveWords sensitiveWords : sensitiveWordsSet) {
                                sensitiveWordsList.add(sensitiveWords);
                            }
                        } else {
                            sensitiveWordsList = sensitiveWordsMapper.selectAll();
                        }
                        redisTemplate.boundValueOps(AllComment.selectAllSitiveWords).set("0");
                        return sensitiveWordsList;
                    } else {
                        throw new RuntimeException("数据库没有数据");
                    }
            } else {
                throw new RuntimeException("正在查找敏感词汇");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            return sensitiveWordsList;
        }
    }

    //6.显示分页敏感词汇 新增SensitiveWords对象,里面含有id和敏感词汇字段,默认为从1开始,size默认为10,可传参
    @Override
    public List<SensitiveWords> selectSomeSensitiveWords(Integer start, Integer size) {
        if (null==start||start<=0){
            start=1;
        }
        if (null==size||size<=0){
            size=10;
        }
        start=start.intValue();
        size=size.intValue();
       String o=(String) redisTemplate.boundValueOps(AllComment.selectSomeSitiveWords).get();
        List<SensitiveWords> sensitiveWordses= new ArrayList<SensitiveWords>();
        try {
            if ("0".equals(o)){
               redisTemplate.boundValueOps(AllComment.selectSomeSitiveWords).set("1");
               PageHelper.startPage(start,size);
               Page<SensitiveWords> tempsensitiveWordses =(Page<SensitiveWords>) sensitiveWordsMapper.selectAll();
               sensitiveWordses = tempsensitiveWordses;
               redisTemplate.boundValueOps(AllComment.selectSomeSitiveWords).set("0");
               return sensitiveWordses;
           }else{
               throw new RuntimeException("正在查询,请稍后");
           }
        } catch (RuntimeException e) {
            e.printStackTrace();
            return sensitiveWordses;
        }
    }
}
